package rs.etf.es.algorithm.states;

import java.util.ArrayList;

import rs.etf.es.algorithm.Algorithm;
import rs.etf.es.algorithm.AlgorithmState;
import rs.etf.es.algorithm.Constants;
import rs.etf.es.algorithm.JTextAreaLogger;
import rs.etf.es.algorithm.Node;
import rs.etf.es.algorithm.NodeAddressBook;

public class SetDefaultLambdaMessagesState implements AlgorithmState {

    private Algorithm algorithm;

    @Override
    public void performAction() {
	JTextAreaLogger.instance().log("");
	ArrayList<Node> nodes = NodeAddressBook.instance().getAllNodes();

	for (Node i : nodes) {
	    ArrayList<Node> allParents = i.getParents();
	    for (Node parent : allParents) {
		for (String val : parent.getValues()) {
		    parent.getLambdaMsgs().addLambdaMessage(i.getName(), val, Constants.DEFAULT_LAMBDA_MESSAGE);
		    JTextAreaLogger.instance().log("Setting \u03BB lambda message from " + i.getName() + " to " + parent.getName() + " for value " + val + " to 1");
		    // AlgorithmBlocker.instance().stopHere();
		}
	    }
	}

	SetInitialRootProbabilitiesAndPiValuesState newState = new SetInitialRootProbabilitiesAndPiValuesState();
	newState.setAlgorithm(algorithm);
	algorithm.pushAlgorithmState(newState);
    }

    @Override
    public void setAlgorithm(Algorithm algorithmClass) {
	algorithm = algorithmClass;
    }

    public AlgorithmState clone() {
	SetDefaultLambdaMessagesState newState = new SetDefaultLambdaMessagesState();
	newState.setAlgorithm(algorithm);
	return newState;
    }

    @Override
    public void refreshNodes() {

    }

}
